﻿using FirstFloor.ModernUI.Windows.Controls;
using MetroGS.AppClass;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MetroGS.Pages
{
    /// <summary>
    /// Interaction logic for UCGames.xaml
    /// </summary>
    public partial class UCGames : UserControl
    {
        SQL ClassSQL = new SQL();
        DateTime starttime;

        string sql;

        public UCGames()
        {
            InitializeComponent();
        }

        private void UC_Games_Initialized(object sender, EventArgs e)
        {
            sql = "SELECT * FROM gamecentral ORDER BY gamename";
            ClassSQL.gamesfill(this, sql);
        }

        private void lv_gamelist_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lv_gamelist.SelectedIndex != -1)
            {
                ClassSQL.datarefresh(this);
            }
        }

        int initcom = 0;
        private void tb_comstats_GotFocus(object sender, RoutedEventArgs e)
        {
            if (initcom == 0)
            {
                tb_comstats.Text = "";
                initcom = 1;
            }
        }

        int playing = 0;
        private void bu_launch_Click(object sender, RoutedEventArgs e)
        {
            if (playing == 0)
            {
                bu_launch.Content = "Commit Playtime";
                tb_comstats.Visibility = Visibility.Visible;

                string gamename = lv_gamelist.SelectedItem.ToString();
                string sqlcm = "SELECT * FROM gamecentral WHERE gamename = '" + gamename + "'";
                ClassSQL.gamestart(sqlcm);

                starttime = DateTime.Now;

                playing = 1;
            }
            else
            {
                bu_launch.Content = "Launch the game";
                tb_comstats.Visibility = Visibility.Hidden;

                DateTime stoptime = DateTime.Now;
                TimeSpan timespan = stoptime - starttime;

                DateTime.Now.ToShortDateString();

                string timeplayedpass1 = new DateTime(timespan.Ticks).ToString("HH");
                string timeplayedpass2 = new DateTime(timespan.Ticks).ToString("mm");

                int timeplayedpass1_1 = Convert.ToInt32(timeplayedpass1) * 60;
                int timeplayedpass = timeplayedpass1_1 + Convert.ToInt32(timeplayedpass2);

                string gamename = lv_gamelist.SelectedItem.ToString();

                int playday = starttime.Day;

                CultureInfo ciCurr = CultureInfo.CurrentCulture;
                int weekNum = ciCurr.Calendar.GetWeekOfYear(starttime, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
                int playweek = weekNum;

                int playmonth = starttime.Month;
                int playyear = starttime.Year;

                string comstat = tb_comstats.Text;

                if (tb_comstats.Text != "" && tb_comstats.Text != "Completion Status")
                {
                    var result = ModernDialog.ShowMessage("You have played " + timeplayedpass + " minutes and completed the game on " + tb_comstats.Text + ".", "Confirmation", MessageBoxButton.OKCancel);
                    if (result == true)
                    {
                        sql = @"INSERT INTO timetrack([gamelink], [sessiontime], [playday], [playweek], [playmonth], [playyear], [completedstatus], [completedtype])VALUES('" + gamename + "', '" + timeplayedpass + "', '" + playday + "', '" + playweek + "', '" + playmonth + "', '" + playyear + "', 'Yes', '" + comstat + "')";
                        ClassSQL.gamesupdate(sql);
                        ClassSQL.gamescomplecal(gamename);

                        sql = @"UPDATE gamecentral SET lastplayed='" + DateTime.Now.ToString("D") + "', lastplayedweek='" + playweek.ToString() + "', lastplayedmonth='" + playmonth.ToString() + "' WHERE gamename = '" + gamename + "'";
                        ClassSQL.gamesupdate(sql);

                        //ModernDialog.ShowMessage("Your playtime has been saved.", "Playtime Updated", MessageBoxButton.OK);
                    }
                    else
                    {
                        //ModernDialog.ShowMessage("Your playtime has not been saved.", "Update Cancel", MessageBoxButton.OK);
                    }
                }
                else
                {
                    var result = ModernDialog.ShowMessage("You have played " + timeplayedpass + " minutes.", "Confirmation", MessageBoxButton.OKCancel);
                    if (result == true)
                    {
                        sql = @"INSERT INTO timetrack([gamelink], [sessiontime], [playday], [playweek], [playmonth], [playyear])VALUES('" + gamename + "', '" + timeplayedpass + "', '" + playday + "', '" + playweek + "', '" + playmonth + "', '" + playyear + "')";
                        ClassSQL.gamesupdate(sql);

                        sql = @"UPDATE gamecentral SET lastplayed='" + DateTime.Now.ToString("D") + "', lastplayedweek='" + playweek.ToString() + "', lastplayedmonth='" + playmonth.ToString() + "' WHERE gamename = '" + gamename + "'";
                        ClassSQL.gamesupdate(sql);

                        //ModernDialog.ShowMessage("Your playtime has been saved.", "Playtime Update", MessageBoxButton.OK);
                    }
                    else
                    {
                        //ModernDialog.ShowMessage("Your playtime has not been saved.", "Update Cancel", MessageBoxButton.OK);
                    }
                }

                tb_comstats.Text = "Completion Status";

                ClassSQL.datarefresh(this);
                playing = 0;
            }
        }
    }
}